/*
 * RFID.c
 *
 * Created: 9/7/2012 8:30:52 AM
 *  Author: duerk (duerk@msoe.edu)
 *			zimmermane (zimmerman@msoe.edu)
 */ 

#define F_CPU 16E6         // CPU clock speed for delay.h

#include <avr/io.h>        //to use named components on Atmega 32
#include <util/delay.h>    //for use of delay functions
#include <avr/interrupt.h> //will include the interrupt vectors
#include <stdint.h>        //for uint8_t
#include <stdbool.h>       //for boolean
#include "RFID.h"
#include "LCD_3200.h"
#include "USART_3200.h"


static volatile bool buttonPressed; //used to tell if INT0 is pressed
static volatile bool heartBeat; //used to turn on and off the heartbeat
static volatile char data_byte_ready[13]; //string of data holding
										  //RFID number


int main(void){
	//initialize the system
	INIT_System();
	//forever loop to update and track program
    while(1){
		/*
		//check to see if the INT0 button was pressed
		if(buttonPressed){
			_delay_ms(20);
			enable_RFID();
		}
		else{
			_delay_ms(20);
			disable_RFID();
		}*/
		/*
		if(heartBeat){
			gotoxy(1,15);
			usart_send_data(0x01);
		}
		else{
			gotoxy(1,15);
			usart_send_data(' ');
		}
		*/
		PORTB = 0xFF;
		_delay_ms(1000);
		PORTB = 0x00;
		_delay_ms(1000);						
    }
}

/*
 * INIT_System
 * Purpose: This will initialize the system
 * Parameters: None
 * Returns: None
 */
void INIT_System(){
	//setup default values
	buttonPressed = false;
	heartBeat = false;
	
	//initialize system items
	INIT_Timer_1();
	INIT_INT0();
	INIT_USART();
	INIT_LCD();
	//display_welcome(); // display welcome screen
	
	//turn Portb to output
	DDRB = 0xFF;
	// turn INT0 pullup on (PD2)
	PORTD = PORTD | (1<<PD2);
	
	//Enable global interrupts
	sei();
}

/* INIT_Timer_1
 * Purpose: to setup Timer 1 to interrupt every 500ms
 * Parameters: None
 * Returns: None
 */
void INIT_Timer_1(){
	OCR1AH = 0x1E; //7813
	OCR1AL = 0x85;
	//start timer at 0
	TCNT1H = 0;
	TCNT1L = 0;
	// initialize tccr1b: CTC mode, clk/1024
	TCCR1B = (1<<WGM12) | (1<<CS12) | (1<<CS10);
	//set Timer counter 1 Output Compare for interrupt
	TIMSK = (1<<OCIE1A);
}

/* INIT_INT0
 * Purpose: to enable interrupts for Pushbuttons and edges
 * Parameters: None
 * Returns: None
 */
void INIT_INT0(){
	//turn on INT0 interrupt
	GICR = (1<<INT0);
	//sets to falling edge trigger for INT0
	MCUCR = (MCUCR & 0xF0) | (0<<ISC01);
}

/*
 * enable_RFID
 * Purpose: This will turn on and enable the RFID reader
 * Parameters: None
 * Returns: None
 */
void enable_RFID(){
	//set baud rate for the on the LCD
	set_baud(LCD_VALUE);
	//send antenna on signal
	usart_send_data(0x03);
	//set the baud rate for the RFID
	set_baud(RFID_VALUE);
	//turn on RFID antenna (active LOW)
	PORTB = PORTB & ~(1<<PB0);
}

/*
 * disbale_RFID
 * Purpose: This will turn off and disable the RFID reader
 * Parameters: None
 * Returns: None
 */
void disable_RFID(){
	set_baud(RFID_VALUE);
	//turn off RFID antenna
	PORTB = PORTB | (1<<PB0);
	//set baud rate for the
	set_baud(LCD_VALUE);
	//send antenna off signal on LCD
	usart_send_data(0x05);
	//set the baud rate for the RFID
}

/*
 * display_Tag
 * Purpose: This will display the information stored on the RFID tag
 * Parameters: None
 * Returns: None
 */
void display_Tag(){
	
}

/*
 * INT0_vect
 * Purpose: This will handle the events of the INT0 pushbutton
 */
ISR(INT0_vect){
	//toggle the button pressed
	buttonPressed = !(buttonPressed);
}

/* 
 * TIMER1_COMPA_vect
 * Purpose: This will handle the heartbeat being displayed
 *			on the lcd every half a second.
 */
ISR(TIMER1_COMPA_vect){
	// toggle the heartbeat
	heartBeat = !heartBeat;
}	

/*
 * USART_RXC_vect
 * Purpose:
 */
ISR(USART_RXC_vect){
	
}